//required imports for tacTile

import processing.net.*;
import tacTile.net.*;

TouchAPI tacTile;


//boolean to determine which Touch Server connected to.
boolean connectToTacTile = true;

//Names of machines you might use
String localMachine = "127.0.0.1";
String tacTileMachine = "tactile.evl.uic.edu";

//Port for data transferf
int dataPort = 7100;
int msgPort = 7340;

//variables
float dX,dY,towerX, towerY, towerX2;
//list of arrows
ArrayList arrows = new ArrayList();

ArrayList monsters = new ArrayList();

ArrayList towers = new ArrayList();

ArrayList buttons = new ArrayList();

Tower P1bow1;
Tower P1cat1;
Tower P1big1;
Tower P1bow2;
Tower P1cat2;

Tower P2bow1;
Tower P2cat1;
Tower P2big1;
Tower P2bow2;
Tower P2cat2;

Button P1inf;
Button P1tank;
Button P1BC;

Button P2inf;
Button P2tank;
Button P2BC;


void setup() {
    //resolution
  size(screen.width, screen.height);
  //set x and y coordinates
  towerX = width/10;
  
  towerX2 = width - width/8;
  
  towerY = height-width/12;
  
   P1inf = new Button(25,25,50,50,"inf");
   P1tank = new Button(25,height/2,50,50,"tank");
   P1BC = new Button(25,height-25,50,50,"BC");
   
   P2inf = new Button(width-25,25,50,50,"inf");
   P2tank = new Button(width-25,height/2,50,50,"tank");
   P2BC = new Button(width-25,height-25,50,50,"BC");
  
   P1bow1 = new Tower(towerX,towerY,85,85,0,1,true,"bow");
   P1cat1 = new Tower(towerX,((towerY+height/2)/2),85,85,5,10,true,"catapult");
   P1big1 = new Tower(towerX,height/2,100,100,10,40,true,"cannon");
   P1bow2 = new Tower(towerX, (height-towerY), 85, 85,0,1,true,"bow");
   P1cat2 = new Tower(towerX, ((height-towerY + height/2)/2), 85, 85,10,40,true,"catapult");  
   
   P2cat1 = new Tower(towerX2,((towerY+height/2)/2),85,85,5,10,true,"catapult");
   P2big1 = new Tower(towerX2,height/2,100,100,10,40,true,"cannon");
   P2bow2 = new Tower(towerX2, (height-towerY), 85, 85,0,1,true,"bow");
   P2cat2 = new Tower(towerX2, ((height-towerY + height/2)/2), 85, 85,10,40,true,"catapult"); 
   P2bow1 = new Tower(towerX2,towerY,85,85,0,1,true,"bow");
   
   towers.add(P1cat1);
   towers.add(P1cat2);
   towers.add(P1big1);
   towers.add(P1bow1);
   towers.add(P1bow2);
   
   towers.add(P2cat1);
   towers.add(P2cat2);
   towers.add(P2big1);
   towers.add(P2bow1);
   towers.add(P2bow2);
   
   buttons.add(P1inf);
   buttons.add(P1tank);
   buttons.add(P1BC);
   
   buttons.add(P2inf);
   buttons.add(P2tank);
   buttons.add(P2BC);

   
   //draw middle line
   line(width/2, 0, width/2, height);
  
  
if ( connectToTacTile ){
      //ALTERNATIVE: constructor to setup the connection on TacTile
      //tacTile = new TouchAPI( this );

      //Create connection to Touch Server
      tacTile = new TouchAPI( this, dataPort, msgPort, tacTileMachine);
      
      tacTile.log_on(true);
      
      //System.out.println(tacTile.log);
      //size of the screen
      size( screen.width, screen.height );

  } else {
      //ALTERNATIVE: constructor to setup the connection LOCALLY on your machine
      //tacTile = new TouchAPI( this, 7000);

      //Create connection to Touch Server
     
     //tacTile = new TouchAPI( this, dataPort, msgPort, localMachine);

      //size of the screen
      size( screen.width, screen.height );
  }
  
  //tacTile.toggleLogging();

  //color of the background
  background( 255 , 255 , 255 );
}

void draw(){
       //color of the background
  background( 255 , 255 , 255 );
     //draw middle line
   line(width/2, 0, width/2, height);

  
  
  //the process() method generates a listing of new, expired, and moved touches. 
  //It must be called before attempting to use the following methods.
  //if you plan to use a combination of getManagedList and these methods,
  //getManagedList calls process() for you so you don't have to.
  tacTile.process();
  
  //Gives you expired touches. Alternate method name: getExpiredTouches()
  ArrayList newUp = tacTile.getTouchesUp();         
  //gives you new touches. Alternate method name: getNewTouches()
  ArrayList newDown = tacTile.getTouchesDown();
  //gives you touches that have moved. Alternate method name: getTouchesMoved()
  ArrayList newMoved = tacTile.getMovedTouches();
  
  
  //loop through each list, getting the touch, assign it a color, and
  //display it to screen. Each list has its own ellipse shape. 
  if( !newUp.isEmpty() ){
     for(int i = 0; i < newUp.size(); i++){
     Touches touch = ((Touches) newUp.get(i));
        
      int finger = touch.getFinger();
      for(int j = 0; j < towers.size(); j++)
      {
        Tower t = (Tower)towers.get(j);
        if(t.getFingerID()==finger)
        {
           t.setEnabled(false);
           t.setFingerID(-1);
           t.setMyTouch(null);
        } 
      }
     } 
  }
  
 if( !newDown.isEmpty() ){
     for(int i = 0; i < newDown.size(); i++){
      Touches touch = ((Touches) newDown.get(i));
      
      float xCoord = touch.getXPos() * width;    
      float yCoord = height - touch.getYPos() * height;
      
      int finger = touch.getFinger();
      
      for(int j = 0; i < towers.size(); j++)
      {
        Tower t = (Tower)towers.get(j);
        if(xCoord<t.x+25&&xCoord>t.x-25&&yCoord>t.y-25&&yCoord<t.y+25)
        {
          t.setEnabled(true);
          t.setFingerID(finger);
          t.setMyTouch(touch);
        }
      }
      

     
   } 
  }
  
 if( !newMoved.isEmpty() ){
     for(int i = 0; i < newMoved.size(); i++){
       Touches t = ((Touches) newMoved.get(i));
        
      int finger = t.getFinger();
  
      
      
     } 
  }
  
  
  //draw middle line
  line(width/7, 0, width/7, height);
  //draw middle line
  line(width-width/6, 0, width-width/6, height);
  
  for(int i = 0; i < towers.size(); i++)
  {
    Tower t = (Tower)towers.get(i);
    t.update(); 
  }
  
  for(int i = 0; i < buttons.size(); i++)
  {
    Button b = (Button)buttons.get(i);
    b.update(); 
  }
  
  
  
}
